/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode *root, int sum) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        return dfs(root, 0, sum);

    }

    bool dfs(TreeNode *root, int curSum, int target) {
        if (root == NULL) {
            return false;
        }
        if (!root->left && !root->right && curSum + root->val == target) {
            return true;
        }
        if (root->left) {
            if (dfs(root->left, curSum + root->val, target)) {
                return true;
            }
        }
        if (root->right) {
            if (dfs(root->right, curSum + root->val, target)) {
                return true;
            }
        }
        return false;
    }
};
